Golang flag.FlagSet.Parse() and Parsed() functions example
package flag
Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help was set but not defined.
Golang flag.FlagSet.Parse() function usage example
package main
import (
"flag"
"fmt"
)
var (
flagSet *flag.FlagSet
)
func init() {
flagSet = flag.NewFlagSet("example", flag.ContinueOnError)
}
func main() {
flagSet.String("file", "", "filename to view")
flagSet.Int("timeout", 5000, "specify the time in milliseconds to wait for a response")
err := flagSet.Parse([]string{"file","timeout"})
if err != nil {
fmt.Println(err)
}
fmt.Println("Parsed the flag ? :", flagSet.Parsed())
}
Output :
Parsed the flag ? : true
References :
Advertisement
Something interesting
Tutorials
+14.9k Golang : How to check for empty array string or string?
+5.4k Golang : Get S3 or CloudFront object or file information
+9.6k Golang : Quadratic example
+6.9k Golang : Normalize email to prevent multiple signups example
+10.4k Golang : cannot assign type int to value (type uint8) in range error
+14.2k Golang : Chunk split or divide a string into smaller chunk example
+9.7k Golang : Format strings to SEO friendly URL example
+31.7k Golang : How to convert(cast) string to IP address?
+6.8k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12.7k Golang : Sort and reverse sort a slice of bytes
+15.7k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+19.5k Golang : Example for DSA(Digital Signature Algorithm) package functions